package com.litong.litongjiaoyu.config.dao;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.beans.PropertyVetoException;

/**
 * @ClassName DataSourceConfiguration
 * @Description TODO
 * @Author jingz
 * @Date 2019/7/15 12:36
 * @Version 1.0
 **/
@Configuration
@MapperScan("com.litong.litongjiaoyu.dao")
public class DataSourceConfiguration {
    @Value("${spring.datasource.driver-class-name}")
    private String jdbcDriver;
    @Value("${spring.datasource.url}")
    private String jdbcUrl;
    @Value("${spring.datasource.username}")
    private String jdbcUserName;
    @Value("${spring.datasource.password}")
    private String jdbcPassword;

    @Bean(name="dataSource")
    public ComboPooledDataSource createDataSource() throws PropertyVetoException {
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        comboPooledDataSource.setDriverClass(jdbcDriver);
        comboPooledDataSource.setJdbcUrl(jdbcUrl);
        comboPooledDataSource.setUser(jdbcUserName);
        comboPooledDataSource.setPassword(jdbcPassword);
        
        //连接池初始化时创建的连接数
        comboPooledDataSource.setInitialPoolSize(6);
        //连接池保持的最小连接数
        comboPooledDataSource.setMinPoolSize(5);
        //连接池中拥有的最大连接数，如果获得新连接时会使连接总数超过这个值则不会再获取新连接，而是等待其他连接释放，所以这个值有可能会设计地很大
        comboPooledDataSource.setMaxPoolSize(10);
        //连接的最大空闲时间，如果超过这个时间，某个数据库连接还没有被使用，则会断开掉这个连接如果为0，则永远不会断开连接
        comboPooledDataSource.setMaxIdleTime(60);
        comboPooledDataSource.setMaxConnectionAge(120);
        comboPooledDataSource.setMaxIdleTimeExcessConnections(50);

        //关闭连接后手动提交
        comboPooledDataSource.setAutoCommitOnClose(false);
        return comboPooledDataSource;
    }

}
